<template>
  <div class="amap-page-container">
    <div class="toolbar" v-show="false" @click="getCurrentPost">
        <span v-if="!loaded">
          location: lng = {{ lng }} lat = {{ lat }} <br>
          address: {{ province }} {{ city }} {{ district }} {{ street }}
        </span>
      <span v-else>正在定位</span>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  // isIOS
  import StoreSave from '@/utils/store'
  import { getLat, setLat, getLng, setLng } from '@/utils/auth'
  // import { TMap } from '@/utils/map'
  import { mapGetters } from 'vuex'
  import { isWeixinCient } from '@/utils/validate'
  import { getLocationInformation } from "@/api/shop/index.js";
  export default Vue.extend({
    name: 'index',
    props: {
      showLoading: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        lng: 0,
        lat: 0,
        loaded: true,
        province: '',
        city: '',
        district: '',
        street: '',
        township: ''
      }
    },
    computed: {
      ...mapGetters(['wxConfig'])
    },
    mounted() {
      // TMap()
      if (this.showLoading) {
        this.$Error('定位中...')
      }
      setTimeout(() => {
        this.defaultLocal()
      }, 1000)
    },
    methods: {
      // 不是当天就清空缓存
      isClearLocationInfo() {
        const now = new Date()
        const year = now.getFullYear()
        const month = now.getMonth() + 1
        const date = now.getDate()
        const newDate = `${year}-${month}-${date}`
        const oldDate = StoreSave.fetchLocal('dateInfo')
        if (oldDate != newDate) {
          setLat('')
          setLng('')
          StoreSave.saveLocal('dateInfo', newDate)
        }
      },
      defaultLocal(type) {
          this.getCurrentPost()
      },
      getCurrentPost() {
        // TODO 小程序获取位置信息
          wx.getLocation({
            type: 'gcj02',
            success: (res) => {
              // console.log(res, '小程序定位')
              this.loaded = false
              this.lng = res.longitude
              this.lat = res.latitude
              setLat(this.lat)
              setLng(this.lng)
              this.getLocationInfo(this.lat, this.lng)
            },
            fail: (res) => {
              // console.log(res,'res111')
              this.$Error('微信获取定位失败,请确认是否已开启定位权限')
              // wx.openSetting()  // 2.3.0 版本开始，用户发生点击行为后，才可以跳转打开设置页，管理授权信息。
            }
          })
      },
      // QQ地图根据经纬度获取实际位置
      getLocationInfo(Lat, Lng) {
        const self = this
          // 小程序根据经纬度请求接口获取实际位置
          getLocationInformation({
            latitude: Lat,
            longitude: Lng
          }).then(res => {
            // console.log(res,'位置信息111')
            if (res.status == 1) {
              const result = res.data.result
              this.province = result.address_component.province
              this.city = result.address_component.city
              this.district = result.address_component.district
              this.street = result.address_component.street
              this.$emit('getLoaction', result)
            }
          })
        self.$emit('getLatLng', {'lat': Lat, 'lng': Lng})
      }
    }
  })
</script>

<style lang="scss">

</style>
